<?php 
namespace app\admin\controller;

class Owner extends Comm{
	//委託列表
	public function owner_list(){
		$controller = request()->controller();
		$action = request()->action();
		$controller = model('Role')->where(['controller'=>$controller])->find()->rname;
		$action = model('Role')->where(['action'=>$action])->find()->rname;
		if(request()->ispost() && $_POST['cond'] != 'all'){
			$key = input('post.key');
			if(input('post.cond') == 'status'){
				$tmp = ['待审核','待出租','已出租'];
				$key = array_search($key,$tmp);
			}
			if(input('post.cond') == 'area'){
				foreach(model('Village')->select() as $val){
					$tmp[] = $val->name;
				}
				array_unshift($tmp,'江苏省');
				$key = array_search($key,$tmp);
			}
			$where = [
				input('post.cond')=>$key
			];
			$res = model('Owner')->alias('o')->join('village v','o.area=v.id')->field('oid,thumb,v.name area,vill,addr,oname,otime,status')->where($where)->select();
			if($res == []){
				$this->assign('data',$res);
				$this->assign('controller',$controller);
				$this->assign('action',$action);
				return $this->fetch();
			}
		}else{
			$res = model('Owner')->alias('o')->join('village v','o.area=v.id')->field('oid,thumb,v.name area,vill,addr,oname,otime,status')->select();
		}
		
		foreach($res as $v){
			$data[] = $v->toArray();
		}
		$this->assign('data',$data);
		$this->assign('controller',$controller);
		$this->assign('action',$action);
		return $this->fetch();
	}

	//添加委托
	public function owner_add(){
		$character = model('OwnerCharacter')->select();
		$ini = model('OwnerIni')->select();
		foreach($character as $v){
			$data_character[] = $v;
		}
		foreach($ini as $v){
			$data_ini[] = $v;
		}
		$this->assign('characters',$data_character);
		$this->assign('inis',$data_ini);
		return $this->fetch();
	}

	//处理添加
	public function do_add(){
		$re = $this->validate(input('post.'),'Owner');
		if($re !== true){
			$this->error($re);
		}
		$files = request()->file('thumb');
		$path = ROOT_PATH.'public'.DS.'static'.DS.'upload'.DS.'owner';
		foreach($files as $file){
			$info = $file->move($path);
			if($info){
				$savename[] = $info->getSavename();
			}else{
				echo $file->getError();
			}
		}
		$_POST['ini'] = implode(',',$_POST['ini']);
		$_POST['character'] = implode(',',$_POST['character']);
		$_POST['thumb'] = $savename[0];
		$_POST['imgs'] = implode(',',$savename);
		$_POST['otime'] = date('Y-m-d H:i:s',time());
		$res = model('Owner')->allowfield(true)->save($_POST);
		if($res > 0){
			$this->success('成功');
		}
	}

	//处理删除
	public function do_del(){
		if(request()->ispost()){
			$where = [
				'oid'=>['in',$_POST['oid']]
			];
			$res = model('Owner')->where($where)->select();
			$count = model('Owner')->where($where)->delete();
			foreach($res as $re){
				$data[] = $re['imgs'];
				$imgs = explode(',',$re['imgs']);
				foreach($imgs as $img){
					$path = ROOT_PATH.'public'.DS.'static'.DS.'upload'.DS.'owner'.DS.$img;
					unlink($path);
				}	
			}
			if($count > 0){
				$this->success('删除成功');
			}else{
				$this->error('删除失败');
			}
		}
	}

	//编辑委托
	public function owner_edit(){

		//改编区域显示
		foreach(model('Village')->where(['pid'=>'1'])->select() as $val){
			$data_area[] = $val->toArray();
		}
		// dump($data_area);
		$this->assign('data_area',$data_area);
		
		if(request()->isget()){
			$oid = $_GET['oid'];
			$list = model('Owner')->alias('o')->join('village v','o.area=v.id')->field('oid,thumb,v.id area,vill,addr,oname,otime,status,character,ini,price,state,face,des1,des2')->where(['oid'=>$oid])->select();
			foreach($list as $vo){
				$data = $vo;
			}
			$character = model('OwnerCharacter')->select();
			$ini = model('OwnerIni')->select();
			foreach($character as $v){
				$data_character[] = $v;
			}
			foreach($ini as $v){
				$data_ini[] = $v;
			}
			$this->assign('characters',$data_character);
			$this->assign('inis',$data_ini);
			$this->assign('oid',$oid);
			$this->assign('data',$data);
			return $this->fetch();
		}
	}

	//处理编辑
	public function do_edit(){
		$re = $this->validate(input('post.'),'Owner');
		if($re !== true){
			$this->error($re);
		}
		$files = request()->file('thumb');

			if($files != null){
				$path = ROOT_PATH.'public'.DS.'static'.DS.'upload'.DS.'owner';
			foreach($files as $file){
				$info = $file->move($path);
				if($info){
					$savename[] = $info->getSavename();
				}else{
					echo $file->getError();
				}
			}
			$res = model('Owner')->where(['oid'=>$_POST['oid']])->select();
			foreach($res as $re){
				$data[] = $re['thumb'];
				$imgs = explode(',',$re['thumb']);
				foreach($imgs as $img){
					$path = ROOT_PATH.'public'.DS.'static'.DS.'upload'.DS.'owner'.DS.$img;
					unlink($path);
				}	
			}
			$_POST['thumb'] = $savename[0];
			$_POST['imgs'] = implode(',',$savename);
		}else{
			$_POST['thumb'] = model('Owner')->where(['oid'=>$_POST['oid']])->find()->thumb;
			$_POST['imgs'] = model('Owner')->where(['oid'=>$_POST['oid']])->find()->imgs;
		}
		

		$_POST['ini'] = implode(',',$_POST['ini']);
		$_POST['character'] = implode(',',$_POST['character']);
		$where = ['oid'=>$_POST['oid']];
		
		if(isset($_POST['checker'])){
			$_POST['checker'] = session('uname');
			$_POST['status'] = '1';
			$res = model('Owner')->allowfield(true)->where($where)->update($_POST);
			$_POST['img'] = $_POST['imgs'];
			$_POST['thumb'] = $_POST['thumb'];
			$_POST['desc'] = $_POST['des1'];
			$_POST['trip'] = $_POST['des2'];

			//改编小区显示
			foreach(model('Village')->select() as $val){
				$tmp[] = $val->name;
			}
			array_unshift($tmp,'江苏省');
			$area = array_search($_POST['vill'],$tmp);
			$_POST['vill'] = $area;
			$_POST['time'] = date('Y-m-d H:i:s',time());
			$res2 = model('Renter')->allowfield(true)->save($_POST);
		}else{
			$res = model('Owner')->allowfield(true)->where($where)->update($_POST);
		}
		if($res > 0){
			$this->success('成功');
		}
	}
	
}

 ?>